package com.sanyou.task;

import com.sanyou.enums.MaintainType;
import com.sanyou.pojo.OperLog;
import com.sanyou.service.OperLogService;
import com.sanyou.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.sanyou.pojo.Product;

import java.util.Date;
import java.util.List;

@Component
public class ProductScheduledTask {
    @Autowired
    private ProductService productService;
    @Autowired
    private OperLogService operLogService;

    /**
     * 根据阴极周期将超过周期的阴极板变更为备用状态
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateEquipCycle() {
        /**
         * 获取所有超过阴极周期的阴极板
         */
        List<Product> productList=productService.selectUpdateProduct();
        for (Product product:productList){
            product.setUpdateStateDate(new Date()); //设置更新时间
            product.setProductState(MaintainType.Reserve.getCode()); //设置备用状态
            product.setUserid("system"); //设置更新者为系统
            try {
                productService.updateProduct(product); //更新阴极板
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
